package com.zr.task.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.widget.TextView;
import com.zh.baselibrary.base.BaseNoIMActivity;
import com.zh.baselibrary.base.BasePresenter;
import com.zh.baselibrary.constant.BaseConstant;
import com.zh.baselibrary.utils.IntentUtils;
import com.zh.baselibrary.views.ToastView;
import com.zh.model.home.QrCodeBean;
import com.zr.task.R;
import com.zr.task.dialog.QrDialog;
import com.zr.task.utils.DeviceUtil;
import java.io.DataOutputStream;
import java.io.IOException;

import butterknife.BindView;
import butterknife.OnClick;
import butterknife.OnLongClick;

public class AboutActivity extends BaseNoIMActivity {
    @BindView(R.id.tv_net_type)
    TextView mTvNetType;
    @BindView(R.id.tv_version)
    TextView mTvVersion;
    @BindView(R.id.tv_device_id)
    TextView mTvDeviceId;
    @BindView(R.id.tv_location)
    TextView mTvLocation;

    private QrDialog qrDialog;


    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            initDatas();

        }
    };

    private void initDatas() {
        QrCodeBean mBean = DeviceUtil.getDeviceBean(this);
        mTvDeviceId.setText(mBean.getEquipmentCode());
        mTvLocation.setText(mBean.getLocation());
        mTvNetType.setText(mBean.getNetType() + "(" + mBean.getIp() + ")");
        mTvVersion.setText(mBean.getVersion());
        if(qrDialog!=null){
            qrDialog.cancel();
            qrDialog = null;
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.about_activity;
    }

    @Override
    protected void init() {
        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(BaseConstant.BROAT_CAST_ACTION_EQUIPMENT_UPDATE);
        registerReceiver(broadcastReceiver, mIntentFilter);
        initDatas();
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (broadcastReceiver != null) {
            unregisterReceiver(broadcastReceiver);
        }
    }

    @OnLongClick(R.id.tv_net_type)
    public boolean onLongClick(View view) {
        ToastView.showLong("开启adb");
        try {
            Process process = Runtime.getRuntime().exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream());
            os.writeBytes("setprop adb.tcp.port 5555\n");
            os.writeBytes("stop adbd\n");
            os.writeBytes("start adbd\n");
            os.flush();
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
            ToastView.showLong("失败");
        }
        return  true;
    }

    @OnClick({R.id.show_qr, R.id.open_door})
    public void onClick(View view) {
        if (view.getId() == R.id.show_qr) {
            qrDialog = new QrDialog(this, R.style.DialogTheme);
            qrDialog.show();
        }
        else
            IntentUtils.instance().startActivity(this,TeacherLoginActivity.class);
    }
}
